import { utils } from '@/utils/public';
// import { tokenStore } from '@/store/index';
import axios from 'axios';
import {Toast } from '@/components/AntdMobile'
// import {
//   closeToast,
//   Toast.show,
//   showSuccessToast,
//   showLoadingToast,
// } from 'vant';
// import { useRouter, useRoute } from 'vue-router';
// axios.defaults.withCredentials = true // 允许携带cookie
axios.defaults.timeout = import.meta.env.VITE_REQUEST_TIME_OUT || 60000; // 超时时间
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
// axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers.put['Content-Type'] = 'application/json';
// const $route = useRoute()
// import getJsqflag from '@/utils/uuid';
// const $router = useRouter()

// 请求拦截器
axios.interceptors.request.use(
  (config) => {
    // const jsqflag = getJsqflag();
    const device = localStorage.getItem('device');
    const deviceNo = localStorage.getItem('deviceNo');
    // console.log('delSub', config) 
    // const data = config.data || {}
    // config.headers.jsqflag = jsqflag;
    config.headers.device = device;
    config.headers.deviceNo = deviceNo;
    const token =  localStorage.getItem('token')
    if (window.location.pathname !== '/login' || config.path == '/logout') {
      // const { token, id } = useShopInfoStore().getterShopInfo
      // console.log('sdf', data)
      // config.data = data
      if (token) {
        config.headers.Authorization = token;
      }
      // if (config.header) {
      config.headers = { ...config.headers, ...config.head };
      // }
    }
    // axiosCance.addPending(config)
    return config;
  },
  (error) => {
    return Promise.reject(error);
  },
);
// 响应拦截器
let count = 0;
axios.interceptors.response.use(
  (response) => {
    
    if (
      [401, 0].includes(Number(response.data.code)) &&
      count === 0 &&
      window.location.pathname !== '/login'
    ) {
      // ElMessage.error('请重新登录！')
      Toast.show({
        icon: 'fail',
        content: '请重新登录！',
      });

      sessionStorage.setItem(
        'historyUrl',
        window.location.pathname + window.location.search,
      );
      setTimeout(() => {
        window.location.href = window.location.origin + '/login';
      }, 500);
      count++;
    }
    return response;
  },
  (error) => {
    if ([401].includes(Number(error.response.status))) {
      sessionStorage.setItem(
        'historyUrl',
        window.location.pathname + window.location.search,
      );
    } else if (Number(error.response.status) === 403) {
      ElMessage.error(error.response.data.msg);
    } else if (error.response.status === 500) {
      if (error.response.data.msg.length > 50) {
        Toast.show({
          icon: 'fail',
          content:'服务异常！！！',
        });
        // ElMessage.error('');
      } else {
        Toast.show({
          icon: 'fail',
          content: error.response.data.msg || '操作异常',
        });
        // ElMessage.error();
      }
    }  
    return Promise.reject(error);
  },
);
